﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using HadronData;

namespace Hadron
{
    class GameStateScoreName
        : GameState
    {
        private enum FadeState
        {
            FadeIn,
            Running,
            FadeOut
        }

        private static GameState s_gameState = new GameStateScoreName();

        private FadeState m_fadeState;
        private float m_fStateTimer;
        private String[,] m_arrLetters = new String[4, 10] {
            {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"},
            {"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T"},
            {"U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3"},
            {"4", "5", "6", "7", "8", "9", " ", "DEL", "CLR", "OK"} };
        private int m_nCursorX, m_nCursorY;
        private Vector2 m_vecCursor;
        private bool m_bNeutralController;
        private String m_strName;
        private Texture2D m_textureBackground;
        private Texture2D m_textureCursor;
        private GameFont m_gameFont;

        public static GameState Instance
        {
            get { return s_gameState; }
        }

        private GameStateScoreName()
        {
        }

        public override void OnEnter()
        {
            ContentManager contentManager = m_gameContext.Game.Content;

            m_textureBackground = contentManager.Load<Texture2D>("Graphics\\ScoresBackground");
            m_textureCursor = contentManager.Load<Texture2D>("Graphics\\NameCursor");
            m_gameFont = new GameFont(AssetManager.Fonts.Title);

            m_fadeState = FadeState.FadeIn;
            m_fStateTimer = 0.5f;
            m_nCursorX = m_nCursorY = 0;
            m_vecCursor = new Vector2(400.0f, 300.0f);
            m_bNeutralController = true;

            if (Gamer.SignedInGamers.Count > 0)
                m_strName = Gamer.SignedInGamers[0].Gamertag;
            else
                m_strName = "";
        }

        public override void OnExit()
        {
            m_gameFont = null;

            m_textureBackground = null;
        }

        public override void Update(GameTime p_gameTime)
        {
            float fDeltaTime = (float)p_gameTime.ElapsedGameTime.TotalSeconds;

            m_fStateTimer = Math.Max(0.0f, m_fStateTimer - fDeltaTime);

            if (m_fStateTimer == 0.0f)
            {
                switch (m_fadeState)
                {
                    case FadeState.FadeIn: m_fadeState = FadeState.Running; break;
                    case FadeState.FadeOut: NextState = GameStateScores.Instance; break;
                }
            }

            if (m_gameController.Neutral)
                m_bNeutralController = true;

            if (m_bNeutralController && m_fadeState == FadeState.Running)
            {
                if (m_gameController.RotateLeft)
                {
                    --m_nCursorX;
                    if (m_nCursorX < 0)
                        m_nCursorX = 9;
                    m_bNeutralController = false;
                }
                if (m_gameController.RotateRight)
                {
                    m_nCursorX = (m_nCursorX + 1) % 10;
                    m_bNeutralController = false;
                }
                if (m_gameController.Forward)
                {
                    --m_nCursorY;
                    if (m_nCursorY < 0)
                        m_nCursorY = 3;
                    m_bNeutralController = false;
                }
                if (m_gameController.Backward)
                {
                    m_nCursorY = (m_nCursorY + 1) % 4;
                    m_bNeutralController = false;
                }
                if (m_gameController.Fire || m_gameController.Shield || m_gameController.Start)
                {
                    if (m_nCursorY == 3 && m_nCursorX == 7 && m_strName.Length > 0)
                        m_strName = m_strName.Substring(0, m_strName.Length - 1);
                    else if (m_nCursorY == 3 && m_nCursorX == 8)
                        m_strName = "";
                    else if (m_nCursorY == 3 && m_nCursorX == 9)
                    {
                        m_gameContext.Name = m_strName;
                        m_fadeState = FadeState.FadeOut;
                        m_fStateTimer = 0.5f;
                    }
                    else
                        m_strName += m_arrLetters[m_nCursorY, m_nCursorX];
                    m_bNeutralController = false;
                    AssetManager.Sounds.MenuItem.Play();
                }
            }

            // update cursor
            Vector2 vecTarget = new Vector2(220.0f + m_nCursorX * 40.0f, 200.0f + m_nCursorY * 40.0f);
            Vector2 vecOffset = vecTarget - m_vecCursor;
            m_vecCursor += vecOffset * fDeltaTime * 8.0f;
        }

        public override void Draw(SpriteBatch p_spriteBatch)
        {
            p_spriteBatch.Begin();
            p_spriteBatch.Draw(m_textureBackground, Vector2.Zero, Color.White);

            // title
            m_gameFont.Colour = Color.YellowGreen;
            m_gameFont.Alignment = GameFontAlignment.Centre;
            m_gameFont.DrawString(p_spriteBatch, new Vector2(400.0f, 120.0f),
                "You have achieved a new High Score!");

            // score
            m_gameFont.Colour = Color.OrangeRed;
            m_gameFont.DrawString(p_spriteBatch, new Vector2(400.0f, 155.0f),
                m_gameContext.Score.ToString());

            // letters
            Vector2 vecPosition = Vector2.Zero;
            m_gameFont.Colour = Color.LightGray;
            for (int nIndexY = 0; nIndexY < 4; nIndexY++)
            {
                vecPosition.Y = 200.0f - 8.0f + nIndexY * 40.0f;
                for (int nIndexX = 0; nIndexX < 10; nIndexX++)
                {
                    vecPosition.X = 220.0f + nIndexX * 40.0f;
                    m_gameFont.DrawString(p_spriteBatch, vecPosition, m_arrLetters[nIndexY, nIndexX]);
                }
            }

            // cursor
            p_spriteBatch.Draw(m_textureCursor, m_vecCursor, null, Color.White,
                0.0f, new Vector2(17.0f, 16.0f), 1.0f, SpriteEffects.None, 0.0f);

            // name
            m_gameFont.Colour = Color.YellowGreen;
            m_gameFont.DrawString(p_spriteBatch, new Vector2(400.0f, 400.0f), m_strName);

            // fade in/out
            float fFadeOpacity = 0.0f;
            switch (m_fadeState)
            {
                case FadeState.FadeIn: fFadeOpacity = m_fStateTimer * 2.0f; break;
                case FadeState.Running: fFadeOpacity = 0.0f; break;
                case FadeState.FadeOut: fFadeOpacity = 1.0f - m_fStateTimer * 2.0f; break;
            }
            p_spriteBatch.Draw(AssetManager.Textures.Fade,
                new Rectangle(0, 0, 800, 600),
                new Color(Color.Black, fFadeOpacity));

            p_spriteBatch.End();
        }
    }
}
